<template>
  <el-dialog
    :title="!comment.commentId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="comment" :rules="dataRule" ref="comment" @keyup.enter.native="commentSubmit()" label-width="80px">
    <el-form-item label="用户手机号" prop="userPhone">
      <el-input v-model="comment.userPhone" placeholder="用户手机号"></el-input>
    </el-form-item>
    <el-form-item label="用户姓名" prop="userName">
      <el-input v-model="comment.userName" placeholder="用户姓名"></el-input>
    </el-form-item>
    <el-form-item label="房源Id" prop="roomId">
      <el-input v-model="comment.roomId" placeholder="房源Id"></el-input>
    </el-form-item>
    <el-form-item label="租房Id" prop="rentId">
      <el-input v-model="comment.rentId" placeholder="租房Id"></el-input>
    </el-form-item>
    <el-form-item label="评论时间" prop="commentTime">
      <el-input v-model="comment.commentTime" placeholder="评论时间"></el-input>
    </el-form-item>
    <el-form-item label="评论内容" prop="content">
      <el-input v-model="comment.content" placeholder="评论内容"></el-input>
    </el-form-item>
    <el-form-item label="评分" prop="userScore">
      <el-input v-model="comment.userScore" placeholder="评分"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="commentSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        comment: {
          commentId: 0,
          userPhone: '',
          userName: '',
          roomId: '',
          rentId: '',
          commentTime: '',
          content: '',
          userScore: ''
        },
        dataRule: {
          userPhone: [
            { required: true, message: '用户手机号不能为空', trigger: 'blur' }
          ],
          userName: [
            { required: true, message: '用户姓名不能为空', trigger: 'blur' }
          ],
          roomId: [
            { required: true, message: '房源Id不能为空', trigger: 'blur' }
          ],
          rentId: [
            { required: true, message: '租房Id不能为空', trigger: 'blur' }
          ],
          commentTime: [
            { required: true, message: '评论时间不能为空', trigger: 'blur' }
          ],
          content: [
            { required: true, message: '评论内容不能为空', trigger: 'blur' }
          ],
          userScore: [
            { required: true, message: '评分不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.comment.commentId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['comment'].resetFields()
          if (this.comment.commentId) {
            this.$http({
              url: this.$http.adornUrl(`/user/usercomment/info/${this.comment.commentId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.comment.userPhone = data.userComment.userPhone
                this.comment.userName = data.userComment.userName
                this.comment.roomId = data.userComment.roomId
                this.comment.rentId = data.userComment.rentId
                this.comment.commentTime = data.userComment.commentTime
                this.comment.content = data.userComment.content
                this.comment.userScore = data.userComment.userScore
              }
            })
          }
        })
      },
      // 表单提交
      commentSubmit () {
        this.$refs['comment'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/user/usercomment/${!this.comment.commentId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'commentId': this.comment.commentId || undefined,
                'userPhone': this.comment.userPhone,
                'userName': this.comment.userName,
                'roomId': this.comment.roomId,
                'rentId': this.comment.rentId,
                'commentTime': this.comment.commentTime,
                'content': this.comment.content,
                'userScore': this.comment.userScore
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
